package com.ruoyi.web.controller.sports;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.sports.domain.vo.ApplicationVO;
import com.ruoyi.sports.domain.vo.EventVO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.sports.domain.Application;
import com.ruoyi.sports.service.IApplicationService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 报名参赛记录Controller
 * 
 * @author ruoyi
 * @date 2023-03-08
 */
@RestController
@RequestMapping("/sports/application")
public class ApplicationController extends BaseController
{
    @Autowired
    private IApplicationService applicationService;

    /**
     * 查询报名参赛记录列表
     */
    @PreAuthorize("@ss.hasPermi('sports:application:list')")
    @GetMapping("/list")
    public TableDataInfo list(Application application)
    {
        startPage();
//        List<ApplicationVO> VOList =new ArrayList<>();
        List<Application> list = applicationService.selectApplicationList(application);
//        for (Application a:list){
//            ApplicationVO vo=new ApplicationVO();
//            vo.setOldStatus(a.getStatus());
//            VOList.add(vo);
//        }
        return getDataTable(list);
    }

    /**
     * 查询报名参赛记录列表
     */
    @PreAuthorize("@ss.hasPermi('sports:application:list')")
    @GetMapping("/myList")
    public TableDataInfo myList(Application application)
    {
        startPage();
        List<ApplicationVO> list = applicationService.selectMyApplicationList(application);
        return getDataTable(list);
    }

    /**
     * 导出报名参赛记录列表
     */
    @PreAuthorize("@ss.hasPermi('sports:application:export')")
    @Log(title = "报名参赛记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Application application)
    {
        List<Application> list = applicationService.selectApplicationList(application);
        ExcelUtil<Application> util = new ExcelUtil<Application>(Application.class);
        util.exportExcel(response, list, "报名参赛记录数据");
    }

    /**
     * 获取报名参赛记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('sports:application:query')")
    @GetMapping(value = "/{applicationId}")
    public AjaxResult getInfo(@PathVariable("applicationId") Long applicationId)
    {
        return success(applicationService.selectApplicationByApplicationId(applicationId));
    }

    /**
     * 用户报名参加赛事项目
     */
    @PreAuthorize("@ss.hasPermi('sports:event:enroll')")
    @Log(title = "用户报名参加赛事项目", businessType = BusinessType.INSERT)
    @PostMapping("/enroll")
    public AjaxResult enroll(@RequestBody EventVO event)
    {
        return toAjax(applicationService.enroll(event));
    }

    /**
     * 新增报名参赛记录
     */
    @PreAuthorize("@ss.hasPermi('sports:application:add')")
    @Log(title = "新增报名参赛记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Application application)
    {
        return toAjax(applicationService.insertApplication(application));
    }

    /**
     * 修改报名参赛记录
     */
    @PreAuthorize("@ss.hasPermi('sports:application:edit')")
    @Log(title = "修改报名参赛记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ApplicationVO application)
    {
        return toAjax(applicationService.updateApplication(application));
    }

    /**
     * 删除报名参赛记录
     */
    @PreAuthorize("@ss.hasPermi('sports:application:remove')")
    @Log(title = "报名参赛记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{applicationIds}")
    public AjaxResult remove(@PathVariable Long[] applicationIds)
    {
        return toAjax(applicationService.deleteApplicationByApplicationIds(applicationIds));
    }

    /**
     * 删除报名参赛记录
     */
    @PreAuthorize("@ss.hasPermi('sports:application:remove')")
    @Log(title = "报名参赛记录", businessType = BusinessType.DELETE)
    @PostMapping("/cancel")
    public AjaxResult cancel(@RequestBody Long applicationId)
    {
        return toAjax(applicationService.cancelApplicationByApplicationId(applicationId));
    }
}
